package com.xuecheng.teaching.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xuecheng.commons.constants.WorkConstans;
import com.xuecheng.commons.enums.ErrorCode;
import com.xuecheng.commons.model.dto.*;
import com.xuecheng.commons.model.vo.*;
import com.xuecheng.commons.utils.BeanHelper;
import com.xuecheng.teaching.domain.CourseWorkRec;
import com.xuecheng.teaching.domain.Work;
import com.xuecheng.teaching.domain.WorkRecord;
import com.xuecheng.teaching.mapper.CourseWorkRecMapper;
import com.xuecheng.teaching.service.CourseWorkRecService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.teaching.service.WorkRecordService;
import com.xuecheng.teaching.service.WorkService;
import com.xuecheng.web.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-08-13
 */
@Service
public class CourseWorkRecServiceImpl extends ServiceImpl<CourseWorkRecMapper, CourseWorkRec> implements CourseWorkRecService {

    @Autowired
    private CourseWorkRecService courseWorkRecService;

    //  作业提交记录[按照课程分组]
    @Override
    public ResponseResult workRecord(PageRequest request, WorkCommitDto workCommitDto) {
        //  1.构建条件
        LambdaQueryWrapper<CourseWorkRec> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotBlank(workCommitDto.getCourseName()), CourseWorkRec::getCourseName, workCommitDto.getCourseName());
        //  2.构建分页
        Page<CourseWorkRec> page = new Page<>(request.getPageNo(), request.getPageSize());
        //  3.分页查询
        page = this.page(page, qw);

        List<CourseWorkRec> list = page.getRecords();

        List<WorkRecordVo> voList = new ArrayList<>();
        for (CourseWorkRec courseWorkRec : list) {
            WorkRecordVo vo = BeanHelper.copyProperties(courseWorkRec, WorkRecordVo.class);
            //补充条件
            vo.setTotalUsers(courseWorkRec.getAnswerNumber());
            vo.setCommitedTime(courseWorkRec.getCommitedTime());
            vo.setReviewedTime(courseWorkRec.getReviewedTime());
            voList.add(vo);
        }

        //  4.返回分页结果
        return PageResponseResult.okResult(page.getTotal(), voList);

    }

    @Autowired
    private WorkRecordService workRecordService;

    //  批改作业
    @Override
    public ResponseResult correction(WorkRecordDto dto) {



        //  校验
        if (dto.getCorrectComment() == null ) {
            throw new BusinessException(ErrorCode.PARAMSERROR);//参数错误
        }

        //  查询作业提交信息
        WorkRecord workRecord = workRecordService.getById(dto.getWorkRecordId());

        //  更新作业信息
        workRecord.setStatus(WorkConstans.RECORD_CORRECT_PENDED);   //  更新作业状态
        workRecord.setCorrectComment(dto.getCorrectComment());  //  批改意见
        workRecord.setCorrectionDate(DateUtil.date());  //  批改时间

        //  保存到数据库
        workRecordService.updateById(workRecord);

        //  构建条件
        LambdaQueryWrapper<CourseWorkRec> qw = new LambdaQueryWrapper<>();
        qw.eq(CourseWorkRec::getCoursePubId,workRecord.getCoursePubId());
        CourseWorkRec courseWorkRec = courseWorkRecService.getOne(qw);

        if (ObjectUtil.isNotEmpty(courseWorkRec)){
            //  更新课程作业表里的批阅信息
            courseWorkRec.setReviewedTime(DateUtil.date()); //  最后批阅时间
            courseWorkRec.setTobeReviewed(courseWorkRec.getTobeReviewed()-1); //  待批阅数
            //courseWorkRec.setAnswerNumber(courseWorkRec.getAnswerNumber() - 1); //  答题人数
            //courseWorkRec.setWorkNumber(courseWorkRec.getWorkNumber() - 1); //  作业总数
            courseWorkRecService.updateById(courseWorkRec);
        }

        return ResponseResult.okResult();
    }

//    //  批阅详情-作业列表
//    @Override
//    public ResponseResult workPage(PageRequest params,Long teachplanId) {
//        //  1.构建条件
//        LambdaQueryWrapper<CourseWorkRec> qw = new LambdaQueryWrapper<>();
//        //  2.构建分页
//        Page<CourseWorkRec> page = new Page<>(params.getPageNo(), params.getPageSize());
//        //  3.分页查询
//        page = this.page(page, qw);
//
//        List<CourseWorkRec> courseWorkRecList = page.getRecords();
//
//        return ResponseResult.okResult(page.getTotal());
//    }


}
